package com.yuncheng.spcyApi.spcyService;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.entity.OaProcessBusiness;
import com.yuncheng.oaApi.mapper.OaProcessBusinessMapper;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.flow.SpcyFlowConstant;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;

import com.yuncheng.vo.CurrentUserVo;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 流程业务主表
 * @Author: yuncheng
 * @Date: 2023-10-23
 * @Version: V1.0
 */
@Service
public class SpcyProcessBusinessService {

    @Resource
    @Lazy
    private OaProcessBusinessMapper oaProcessBusinessMapper;

//    @Transactional(rollbackFor = Exception.class)
    public void addSpcyProcess(String fpid,String zy,String title,String flcmb){
        CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
        String sqrid = "";
        String sqrbm = "";
        String sqrxm = "";

        if (currentUser != null){
            sqrid = currentUser.getId();
            sqrxm = currentUser.getName();
            sqrbm = currentUser.getDeptName();
        }

        OaProcessBusiness oaProcessBusiness = new OaProcessBusiness();
        oaProcessBusiness.setId(GetUuIdUtils.ReplaceUuId());

        oaProcessBusiness.setFpid(fpid);
        oaProcessBusiness.setFbt(title);
        oaProcessBusiness.setFzy(zy);

        oaProcessBusiness.setFuserid(sqrid);
        oaProcessBusiness.setFsqr(sqrxm);
        oaProcessBusiness.setFsqbm(sqrbm);

        oaProcessBusiness.setFsqsj(DateConversionUtils.DateToYMDMM(new Date()));

        oaProcessBusiness.setFywlx(SpcyFlowConstant.PROCESS_BUSINESS_YWLX_SPCY);
        oaProcessBusiness.setFlcmb(flcmb);
        oaProcessBusiness.setFcshdb(SpcyConstant.BLZT_DB);

        oaProcessBusinessMapper.insert(oaProcessBusiness);
    }

    public void addOaProcess(String fpid, String zy, String title, String flcmb){

        List<OaProcessBusiness> processBusinessList = oaProcessBusinessMapper.selectList(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, fpid));

        if (CollectionUtil.isNotEmpty(processBusinessList)){
             //当前已经插入的主键id 然后更新 摘要信息
            OaProcessBusiness oaProcessBusiness = processBusinessList.get(0);
            oaProcessBusiness.setFzy(zy);
            oaProcessBusinessMapper.updateById(oaProcessBusiness);

        }else {
            CurrentUserVo currentUser = GetCurrentUserUtils.getCurrentUser();
            String sqrid = "";
            String sqrbm = "";
            String sqrxm = "";

            if (currentUser != null) {
                sqrid = currentUser.getId();
                sqrxm = currentUser.getName();
                sqrbm = currentUser.getDeptName();
            }

            OaProcessBusiness oaProcessBusiness = new OaProcessBusiness();
            oaProcessBusiness.setId(GetUuIdUtils.ReplaceUuId());

            oaProcessBusiness.setFpid(fpid);
            oaProcessBusiness.setFbt(title);
            oaProcessBusiness.setFzy(zy);

            oaProcessBusiness.setFuserid(sqrid);
            oaProcessBusiness.setFsqr(sqrxm);
            oaProcessBusiness.setFsqbm(sqrbm);

            oaProcessBusiness.setFsqsj(DateConversionUtils.DateToYMDMM(new Date()));

            oaProcessBusiness.setFywlx(SpcyFlowConstant.PROCESS_BUSINESS_YWLX_OA);
            oaProcessBusiness.setFlcmb(flcmb);
            oaProcessBusiness.setFcshdb(SpcyConstant.BLZT_DB);

            oaProcessBusiness.setCreateTime(new Date());
            oaProcessBusinessMapper.insert(oaProcessBusiness);
        }
    }

    public void deleteByFpid(String pid){
        oaProcessBusinessMapper.delete(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, pid));
    }
}
